<?php

/**
 * 会员列表
 * File Name：MemberController.php
 * File Encoding：UTF-8
 * File New Time：2014-5-15 9:40:51
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MemberController extends AdminBase {

    //会员列表
    public function actionList() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('list', $_POST));
            exit;
        }
        $where = array(
            // 'select' => 'mm.*,t.*',
            'order' => 'uid desc'
        );
        //类型
        $kind = $_GET['kind'];
        if ($kind != '') {
            $where['t.kind'] = array('EQ', (int) $kind);
            $this->assign('kind', $kind);
        }
        //积分
        $score_floor = (int) $_GET['score_floor'];
        if ($score_floor) {
            $where['score'] = array('EGT', $score_floor);
            $this->assign('score_floor', $score_floor);
            $score_upper = (int) $_GET['score_upper'];
            if ($score_upper) {
                $where['score'] = array(array('EGT', $score_floor), array('ELT', $score_upper), 'AND');
                $this->assign('score_upper', $score_upper);
            }
        }
        //注册时间
        $reg_floor = $_GET['reg_floor'];
        if ($reg_floor) {
            $where['t.regdate'] = array('EGT', strtotime($reg_floor));
            $this->assign('reg_floor', $reg_floor);
            $reg_upper = $_GET['reg_upper'];
            if ($reg_upper) {
                $where['t.regdate'] = array('ELT', strtotime($reg_upper));
                $this->assign('reg_upper', $reg_upper);
            }
        }
        //登录时间
        $login_floor = $_GET['login_floor'];
        if ($login_floor) {
            $where['t.lastlogintime'] = array('EGT', strtotime($login_floor));
            $this->assign('login_floor', $login_floor);
            $login_upper = $_GET['login_upper'];
            if ($login_upper) {
                $where['t.lastlogintime'] = array('ELT', strtotime($login_upper));
                $this->assign('login_upper', $login_upper);
            }
        }
        //搜索类型
        $sType = $_GET['sType'];
        $keyword = trim($_GET['keyword']);
        if ($keyword) {
            if ($sType == 'email') {
                $where['t.email'] = array('EQ', $keyword);
            } else if ($sType == 'uid') {
                $where['t.uid'] = array('EQ', $keyword);
            } else if ($sType == 'username') {
                $where['t.username'] = array('EQ', $keyword);
            } else if ($sType == 'phone') {
                $where['t.phone'] = array('EQ', $keyword);
            } else if ($sType == 'taobao_user_nick') {
                $modeltb = MembersTaobaoModel::model();
                $modeltbObj = $modeltb->find($modeltb->where(array('taobao_user_nick' => $keyword)));
                $key = $keyword;
                $sType = 'uid';
                $keyword = $modeltbObj->uid;
                $where['t.uid'] = array('EQ', $keyword);
                $sType = 'taobao_user_nick';
                $keyword = $key;
            }
            $this->assign('sType', $sType);
            $this->assign('keyword', $keyword);
        }

        $model = MembersModel::model();

        $criteria = $model->where($where);
        //分页数
        $limit = 20;
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //$criteria->join = 'LEFT JOIN {{members_money}} as mm on t.uid=mm.uid';
        $count = $model->count($criteria);
        //分页
        $page = AdminBase::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);
        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render();
    }

    //编辑
    public function actionEdit() {
        $uid = Yii::app()->request->getParam('uid');
        $model = MembersModel::model()->with('MembersMoneyModel')->findByPk($uid);
        if (empty($model)) {
            $this->error('该信息不存在！');
        }
        if (IS_POST) {
            $new_password = $_POST['new_password'];
            $model->attributes = $_POST;
            $birthday = strtotime($_POST['birthday']);
            $model->year = date('Y', $birthday);
            $model->month = date('m', $birthday);
            $model->day = date('d', $birthday);
            $model->phone = Yii::app()->request->getParam('phone');
            if ($new_password) {
                $model->password = $model->hashPassword($new_password);
            }
            if ($model->validate() && $model->save()) {
                $this->success('更新成功！', self::U('list'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $this->assign('data', $model);
            $this->render();
        }
    }

    //删除
    public function actionDelete() {
        $uid = Yii::app()->request->getParam('uid');
        $model = MembersModel::model()->findByPk($uid);
        if (empty($model)) {
            $this->error('该信息不存在！');
        }
        if (MembersModel::model()->deleteUser($model)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    //解除旺旺
    public function actionRelieve_wanwan() {
        $uid = Yii::app()->request->getParam('uid');
        $model = MembersModel::model()->findByPk($uid);
        if (empty($model)) {
            $this->error('该用户不存在！');
        }
        if (MembersTaobaoModel::model()->relieve($uid)) {
            $this->success('解除旺旺绑定成功！');
        } else {
            $this->error('解除失败！');
        }
    }

    //登录用户帐号
    public function actionUserlogin() {
        $uid = (int) $_GET['uid'];
        if (empty($uid)) {
            $this->error('请指定需要登录的会员UID！');
        }
        Yii::app()->user->logout();
        $info = Yii::app()->user->getLocalUser($uid);
        if (empty($info)) {
            $this->error('该用户不存在！');
        }
        Yii::app()->user->registerLogin($info);
        $this->success('登录成功！', self::U('front/index/index'));
    }

}
